﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
/*
public class AssetBunldEditor : EditorWindow
{

	[MenuItem("打包/Bundle File Chaser")]
	public static void InitWindow()
	{
		var wd = EditorWindow.GetWindow<AssetBunldEditor>();
		wd.Show();
	}

	private void OnGUI()
	{
		EditorGUILayout.BeginVertical();
		EditorGUILayout.BeginVertical();
		m_TargerFolder = EditorGUILayout.ObjectField("文件夹", m_TargerFolder, typeof(UnityEngine.Object), false);
		string path = AssetDatabase.GetAssetPath(m_TargerFolder);
		EditorGUILayout.LabelField(path);


		EditorGUILayout.EndVertical();

		EditorGUILayout.EndVertical();
	}

	//System.IO.Directory  
	Object m_TargerFolder;
}
*/

public class AssetBunldEditor : ScriptableWizard{
	[MenuItem("打包/Bundle File Chaser 2")]
	static void Init()
	{
		DisplayWizard<AssetBunldEditor>("Bundle版本文件信息", "退出", "保存");
	}

	private void OnWizardUpdate()
	{
	}

	protected override bool DrawWizardGUI()
	{

		base.DrawWizardGUI();

		//m_TargerFolder = EditorGUILayout.ObjectField("文件夹", m_TargerFolder, typeof(UnityEngine.Object), false);
		//string path = AssetDatabase.GetAssetPath(m_TargerFolder);
		//EditorGUILayout.LabelField(path);
		if (GUILayout.Button("选择文件夹")) {
			var path = EditorUtility.OpenFolderPanel("选择文件夹", "Assets", null);
			if (!string.IsNullOrEmpty(path))
			{
				path = path.Substring(Application.dataPath.Length - 6);
			}
			if (path.StartsWith("Assets"))
			{

				m_FolderPath = path.Replace(@"/", @"\");
				m_IsDirty = true;
			}//string pre = Application.dataPath
		}
		EditorGUILayout.LabelField(m_FolderPath);

		if (!string.IsNullOrEmpty(m_FolderPath) && m_IsDirty) {
			m_DrawPath.Clear();
			string[] files = AssetDatabase.FindAssets(null, new string[] { m_FolderPath });
			foreach (var file in files)
			{
				var p = AssetDatabase.GUIDToAssetPath(file);
				var go = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(p);
				if (null != go && !System.IO.Directory.Exists(p))
				{
					m_DrawPath.Add(p.Replace(@"/", @"\"));
				}

			}
			m_LayouScroll = Vector2.zero;
			m_IsDirty = false;
		}

		if (m_DrawPath.Count > 0)
		{
			m_LayouScroll = EditorGUILayout.BeginScrollView(m_LayouScroll);

			for (int i = 0; i < m_DrawPath.Count; i++)
			{
				EditorGUILayout.BeginHorizontal();
				EditorGUILayout.LabelField(m_DrawPath[i]);
				EditorGUILayout.Space();
				//var go = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(m_DrawPath[i]);
				EditorGUILayout.LabelField(AssetDatabase.AssetPathToGUID(m_DrawPath[i]));
				EditorGUILayout.EndHorizontal();
			}
			
			EditorGUILayout.EndScrollView();
		}
		return true;
	}


	//结束
	private void OnWizardCreate()
	{

	}

	//保存
	private void OnWizardOtherButton()
	{
		//AssetBundleBuild[] assetBundles = new AssetBundleBuild[m_DrawPath.Count];
		var buildList = new List<AssetBundleBuild>();
		for (int i = 0; i < m_DrawPath.Count; i++)
		{
			var import = AssetImporter.GetAtPath(m_DrawPath[i]);
			
			AssetBundleBuild temp = new AssetBundleBuild();
			temp.assetBundleName = import.assetBundleName;
			temp.assetBundleVariant = import.assetBundleVariant;
			temp.assetNames = AssetDatabase.GetAssetPathsFromAssetBundle(import.assetBundleName);
			buildList.Add(temp);
		}


		var mifast = BuildPipeline.BuildAssetBundles(Application.dataPath + "/Bundle", BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows);

		Debug.Log(mifast);
	}


	Vector2 m_LayouScroll = Vector2.zero;

	//System.IO.Directory  
	Object m_TargerFolder;
	string m_FolderPath;

	bool m_IsDirty = false;
	List<string> m_DrawPath = new List<string>(); 
}

